#include "my_win.h"

/* 界面状态定义 */
typedef enum 
{
    VIEW_MAIN,        // 主视图
    VIEW_CONFIG_SU7,  // SU7配置视图
    VIEW_CONFIG_YU7,  // YU7配置视图
    VIEW_CONFIG_SUPER // SU7UItra配置视图
};

/* 当前显示的车型索引 */
static int current_model_index = 0;

/* 当前视图状态 */
static int current_view = VIEW_MAIN;

/* 界面指针 */
static lv_obj_t *main_win   = NULL;
static lv_obj_t *car_img    = NULL;
static lv_obj_t *config_win = NULL;
static lv_obj_t *config_img = NULL;

/* 函数原型声明 */
static void prev_button_callback(lv_event_t *event);
static void next_button_callback(lv_event_t *event);
static void car_image_click_callback(lv_event_t *event);
static void return_button_callback(lv_event_t *event);
static lv_obj_t *create_button(lv_obj_t *parent, int x, int y, int w, int h, const char *text, lv_event_cb_t cb);

/* 图标按钮 */
lv_obj_t * lv_imgbtn(lv_obj_t *parent, int x, int y, int w, int h, const char *pic_path)
{
    /*Darken the button when pressed and make it wider*/
    static lv_style_t style_pr;
    static bool style_init = false;
    
    if (!style_init) 
    {
        lv_style_init(&style_pr);
        lv_style_set_img_recolor_opa(&style_pr, LV_OPA_30);
        lv_style_set_img_recolor(&style_pr, lv_color_black());
        lv_style_set_transform_height(&style_pr, 20);
        style_init = true;
    }

    /*Create an image button*/
    lv_obj_t * imgbtn = lv_imgbtn_create(parent);
    if(imgbtn == NULL) 
    {
        LV_LOG_ERROR("lv_imgbtn_create fail:"); // 输出错误提示
        return NULL;
    }

    lv_obj_set_size(imgbtn, w, h);
    lv_imgbtn_set_src(imgbtn, LV_IMGBTN_STATE_RELEASED, NULL, (char *)pic_path, NULL);
    lv_obj_add_style(imgbtn, &style_pr, LV_STATE_PRESSED);

    lv_obj_align(imgbtn, LV_ALIGN_TOP_LEFT, x, y);

    return imgbtn;
}

/* 创建通用按钮 */
static lv_obj_t *create_button(lv_obj_t * parent, int x, int y, int w, int h, const char *text, lv_event_cb_t cb)
{
    lv_obj_t *btn = lv_btn_create(parent);
    lv_obj_set_size(btn, w, h);
    lv_obj_set_pos(btn, x, y);
    lv_obj_add_event_cb(btn, cb, LV_EVENT_CLICKED, NULL);
    
    lv_obj_t *label = lv_label_create(btn);
    lv_label_set_text(label, text);
    lv_obj_center(label);
    
    return btn;
}

/* 获取车型图片路径 */
static const char *get_car_image_path(int model_index)
{
    static const char *car_images[] = {"S:/root/project/xu/SU7.bmp", 
                                        "S:/root/project/xu/SU7Pro.bmp",
                                        "S:/root/project/xu/SU7Max.bmp"};
    return car_images[model_index];
}

/* 获取配置图片路径 */
static const char *get_config_image_path(int model_index)
{
    static const char *config_images[] = {"S:/root/project/xu/SU7_par.bmp",
                                           "S:/root/project/xu/SU7Pro_par.bmp",
                                           "S:/root/project/xu/SU7Max_par.bmp"};
    return config_images[model_index];
}

/* 更新车型图片 */
static void update_car_image()
{
    const char *current_pic_path = get_car_image_path(current_model_index);
    if(car_img != NULL) 
    {
        lv_img_set_src(car_img, (char *)current_pic_path);
    }
}

/* 更新配置图片 */
static void update_config_image()
{
    const char *current_pic_path = get_config_image_path(current_model_index);
    if(config_img != NULL) 
    {
        lv_img_set_src(config_img, (char *)current_pic_path);
    }
}


/* 显示主界面 */
void show_main_win()
{
    // 如果配置窗口存在，则隐藏它
    if(config_win != NULL) 
    {
        lv_obj_add_flag(config_win, LV_OBJ_FLAG_HIDDEN);
    }
    
    // 如果主窗口不存在，则创建它
    if(main_win == NULL) 
    {
        
        main_win = lv_obj_create(lv_scr_act());
        lv_obj_remove_style_all(main_win);
        lv_obj_set_size(main_win, 800, 480);

        // 创建二维码图片
        lv_obj_t *qr_img = lv_img_create(lv_scr_act());
        lv_obj_set_size(qr_img, 200, 200);
        lv_obj_set_pos(qr_img, 0, 280);
        // 设置图片源
        lv_img_set_src(qr_img, "S:/root/project/xu/qr.bmp"); 

        // 创建车型图片
        car_img = lv_img_create(main_win);
        lv_obj_set_size(car_img, 600, 400);
        lv_obj_set_pos(car_img, 200, 40);
        
        lv_obj_t *car_bnt = lv_btn_create(lv_scr_act());
        lv_obj_set_style_opa(car_bnt, LV_OPA_0, 0);
        lv_obj_set_size(car_bnt, 600, 400);
        lv_obj_set_pos(car_bnt, 200, 40);
        lv_obj_add_event_cb(car_bnt, car_image_click_callback, LV_EVENT_CLICKED, NULL);

        // 创建上一张按钮
        create_button(main_win, 200, 440, 100, 40, "prev", prev_button_callback);
        
        // 创建下一张按钮
        create_button(main_win, 700, 440, 100, 40, "next", next_button_callback);
    }
    
    // 确保主窗口可见
    lv_obj_clear_flag(main_win, LV_OBJ_FLAG_HIDDEN);
    
    // 更新显示的图片
    update_car_image();
    
    current_view = VIEW_MAIN;
}

/* 显示配置界面 */
static void show_config_win()
{
    // 如果主窗口存在，则隐藏它
    if(main_win != NULL) 
    {
        lv_obj_add_flag(main_win, LV_OBJ_FLAG_HIDDEN);
    }
    
    // 如果配置窗口不存在，则创建它
    if(config_win == NULL) 
    {
        config_win = lv_obj_create(lv_scr_act());
        lv_obj_remove_style_all(config_win);
        lv_obj_set_size(config_win, 800, 480);
        
        // 创建配置图片
        config_img = lv_img_create(config_win);
        lv_obj_set_size(config_img, 600, 400);
        lv_obj_set_pos(config_img, 200, 40);
        
        // 创建返回按钮
        create_button(config_win, 700, 440, 100, 40, "return", return_button_callback);
    }
    
    // 确保配置窗口可见
    lv_obj_clear_flag(config_win, LV_OBJ_FLAG_HIDDEN);
    
    // 更新显示的图片
    update_config_image();
    
    // 设置当前视图状态
    current_view = VIEW_CONFIG_SU7 + current_model_index;
}

/* 上一张按钮回调函数 */
static void prev_button_callback(lv_event_t *event)
{
    
    current_model_index--;
    if(current_model_index<0)
    {
        current_model_index = 2;
    }
    update_car_image();
}

/* 下一张按钮回调函数 */
static void next_button_callback(lv_event_t *event)
{
    current_model_index++;
    if(current_model_index>2)
    {
        current_model_index = 0;
    }
    update_car_image();
}

/* 图片点击回调函数 */
static void car_image_click_callback(lv_event_t *event)
{
    // 直接调用显示配置窗口函数
    show_config_win();
}

/* 返回按钮回调函数 */
static void return_button_callback(lv_event_t *event)
{
    show_main_win();
}